package com.hydl.power.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.GatewayService;
import com.hydl.power.utils.IdUtil;
import com.hydl.power.utils.SerialNumberUtils;
import com.hydl.power.utils.SmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GatewayServiceImpl implements GatewayService {
    @Autowired
    private GatewayMapper gatewayMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private PowerRoomMapper powerRoomMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TemperatureDeviceMapper temperatureDeviceMapper;

    @Override
    public List<Gateway> queryGatewayAll() {
        return gatewayMapper.queryGatewayAll();
    }

    @Override
    public int deleteGatewayById(Integer id) {
        return gatewayMapper.deleteGatewayById(id);
    }

    @Override
    public int addGateway(String gatewaySn, String gatewayPwd, String roomId, String projectId,String gatewayRemark,Integer brand) {
        Gateway flag=gatewayMapper.queryGatewayById(gatewaySn);
        if(flag!=null){
            return 4;
        }
        Project project=projectMapper.queryProjectByProId(projectId);
        PowerRoom powerRoom=powerRoomMapper.selectByRoomId(roomId);
        if(project==null || powerRoom==null){
            return 3;
        }
        Gateway gateway=new Gateway();
        gateway.setGatewaySn(SerialNumberUtils.getSerialNumber(gatewaySn));
        gateway.setGatewayPwd(gatewayPwd);
        gateway.setRoomId(roomId);
        gateway.setRoomName(powerRoom.getRoomName());
        gateway.setProjectId(projectId);
        gateway.setProjectName(project.getProjectName());
        gateway.setGatewayRemark(gatewayRemark);
        gateway.setStatus("0");
        gateway.setBrand(brand);

      //添加网关同时 添加10台测温设备
      int i = gatewayMapper.addGateway(gateway);
      //查询下是否有相通网关的设备
      List temperatureDeviceList = temperatureDeviceMapper.getTemperatureDeviceByGatewaySn(gatewaySn);
      if (temperatureDeviceList.size()== 0) {
        //开始添加
      if(i > 0){
        if(project.getPowerDaraFlag().equals("2")){
          for (int j = 1;j<=10;j++){
            TemperatureDevice temperatureDevice=new TemperatureDevice();
            temperatureDevice.setDeviceId(IdUtil.getRandomString(8));
            temperatureDevice.setDeviceName(gatewaySn+"_"+j);
            temperatureDevice.setAreaId(project.getCityId());
            temperatureDevice.setAreaName(powerRoom.getAreaName());
            temperatureDevice.setProjectId(projectId);
            temperatureDevice.setProjectName(project.getProjectName());
            temperatureDevice.setPowerRoomId(powerRoom.getRoomId());
            temperatureDevice.setPowerRoomName(powerRoom.getRoomName());
            temperatureDevice.setDeviceAddress(powerRoom.getAddress());
            temperatureDevice.setSerialNumber(SerialNumberUtils.getSerialNumber(gatewaySn)+"_"+j);
            temperatureDevice.setGatewaySn(gatewaySn);
            temperatureDevice.setDeviceMessageAddress(String.valueOf(j));
            temperatureDevice.setTableName(project.getTemperatureDataTableName());
            temperatureDeviceMapper.addTemperatureDevice(temperatureDevice);
          }
        }else {
          for (int j = 1;j<=10;j++){
            Device device = new Device();
            device.setDeviceName(gatewaySn+"_"+j);
            device.setAreaId(project.getCityId());
            device.setProjectId(projectId);
            device.setPowerRoomId(powerRoom.getRoomId());
            device.setSerialNumber(SerialNumberUtils.getSerialNumber(gatewaySn)+"_"+j);
            device.setGatewaySn(gatewaySn);
            device.setAreaName(powerRoom.getAreaName());
            device.setPowerRoomName(powerRoom.getRoomName());
            device.setNavId(project.getProjectName());
            device.setTableName(project.getTableName());
            device.setFlag(1);
            deviceMapper.insertInto(device);
          }
        }
      }
      }
      return i;
    }


    @Override
    public PageInfo<Gateway> queryByCityId(Integer pageNum, Integer pageSize,String cityId, String projectId, String roomId) {
        List<Gateway> list=gatewayMapper.queryByCityId(pageNum,pageSize,cityId,projectId,roomId);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Gateway> queryByTokenId(Integer pageNum, Integer pageSize, String cityId, String projectId, String roomId, String token) {
        List<Gateway> list=gatewayMapper.queryByTokenId(pageNum,pageSize,cityId,projectId,roomId,token);
        return new PageInfo<>(list);
    }

    @Override
    public List<Gateway> queryGatewayByPowerId(String roomId) {
        return gatewayMapper.queryGatewayByPowerId(roomId);
    }

    @Override
    public List<String> selectGatewaySn() {
        return gatewayMapper.selectGateWaySn();
    }

    @Override
    public PageInfo<Gateway> queryGatewayLike(Integer pageNum,Integer pageSize,String text, String token) {
        SysUser sysUser =sysUserMapper.getUserByToken(token);
        if(sysUser!=null){
            if(sysUser.getStatus()==2) {
                return new PageInfo<>(gatewayMapper.queryGatewayLike(pageNum,pageSize,text,sysUser.getProjectId().toString()));
            }
        }
        return new PageInfo<>(gatewayMapper.queryGatewayLike(pageNum,pageSize,text,null));
    }



    @Scheduled(cron = "0 */3 * * * ?")
    public void examineGateway(){
        List<Gateway> gateways = gatewayMapper.queryGatewayAll();
        for (Gateway gateway:gateways){
            List<Device> deviceList=deviceMapper.getDeviceByGatewaySn(gateway.getGatewaySn());
            int flag=0;
            for (Device device:deviceList){
                Object data = redisTemplate.opsForValue().get(device.getSerialNumber());
                if (data==null){
                    flag++;
                }
            }
            if (flag>=3){
               int i= gatewayMapper.updateGatewayStatus(gateway.getId(),1);
            }else{
                int i= gatewayMapper.updateGatewayStatus(gateway.getId(),0);
            }
        }
    }
  //建业凯旋每两小时发短信
  //@Scheduled(cron = "0 0 0,2,4,6,8,10,12,14,16,18,20,22 * * ? ")
  public void kxgcUser(){
    //发送短信
    Map<String, String> map = new HashMap<String, String>();
    map.put("powerRoomName", "凯旋广场配电房");
    String data = JSONObject.toJSONString(map);
    SmsUtils.sendSms("17803879522","合一电力","SMS_226975161",data);
  }
}
